<?php

namespace App\Http\Controllers;
use App\Repositories\Attendance\AttendanceFilter;
use App\Repositories\Attendance\AttendanceRepository;
use App\Transformers\AttendanceTransformer;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class AttendanceController extends Controller
{
    public function index(AttendanceRepository $attendanceRepository, Request $request)
    {

        $paginator = $attendanceRepository->getPaginator(new AttendanceFilter($request));
        return $this->response()->paginator($paginator, new AttendanceTransformer());
    }

    public function export(AttendanceRepository $attendanceRepository, Request $request){
        $cellData = [
            '考勤序号','姓名','部门',
            '最早打卡时间','最晚打卡时间',
            '出勤时长', '规定出勤时长','百分比'
        ];

        $paginator = $attendanceRepository->getPaginator(new AttendanceFilter($request));
        $formating = collect($paginator)->toArray();
        $formatArr = $formating['data'];
        array_unshift($formatArr,$cellData);

        Excel::create('出勤明细表',function($excel) use ($cellData, $formatArr){
            $excel->sheet('score', function($sheet) use ($cellData, $formatArr){
                $sheet->rows($formatArr);
            });
        })->export('xls');
    }

}


